//
//  XCTabBarView.m
//  CreditAddressBook
//
//  Created by Lee on 15/4/22.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "XCTabBarView.h"
#import "XCTabBarButton.h"

@interface XCTabBarView ()

@property (nonatomic, weak) UIButton *selectedBtn;
@property(nonatomic, assign) BOOL callBtnShow;
@end
@implementation XCTabBarView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        //监听通话，删除按钮隐藏通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBtnChange:) name:CYTabBarChangeNotification object:nil];
    }
    return self;
}

- (void)setSelectedIndex:(LESelectedIndex)selectedIndex {
    _selectedIndex = selectedIndex;
    
    if (self.subviews.count > 0) {
        [self selectButtonWithIndex:selectedIndex - 1];
    }
}

- (void)addTabBarButtonWithImageName:(NSString *)imageName andSelImageName:(NSString *)selImageName andTitle:(NSString *)title {
    XCTabBarButton *tabBtn = [[XCTabBarButton alloc] init];
    [tabBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    if (selImageName) {
        [tabBtn setImage:[UIImage imageNamed:selImageName] forState:UIControlStateSelected];
    }
    [tabBtn setTitle:title forState:UIControlStateNormal];
    [tabBtn setTitleColor:CYColor(110, 110, 110, 1) forState:UIControlStateNormal];
    [tabBtn setTitleColor:SELTitleColor  forState:UIControlStateSelected];
    [self addSubview:tabBtn];
    [self layoutSubviews];
    [tabBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    if (_selectedIndex < LESelectedIndexFirst) {
        _selectedIndex = 2;//设置默认选中第2个按钮
    }
    if (self.subviews.count == _selectedIndex) {
        [self buttonClicked:tabBtn];
    }
     
}

- (void)addTabBarButtonWithTitles:(NSArray *)titles andTitleColor:(UIColor *)color andselColor:(UIColor *)selColor andFont:(UIFont *)font {
//    CGFloat count = titles.count;
//    CGFloat tabBarW = self.frame.size.width/count;
//    CGFloat tabBarH = self.frame.size.height;
    for (NSString *title in titles) {
        XCButton *btn = [[XCButton alloc] init];
        btn.titleLabel.font = font;
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:selColor forState:UIControlStateSelected];
        [btn setTitleColor:color forState:UIControlStateNormal];
        [self addSubview:btn];
        [self layoutSubviews];
        [btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
        //添加底部横线
//        UIView *bottomLine = [[UIView alloc]initWithFrame:CGRectMake(0, tabBarH - 1, tabBarW, 1)];
//        [bottomLine setBackgroundColor:[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.3]];
//        [btn addSubview:bottomLine];
        
        if (_selectedIndex < LESelectedIndexFirst) {
            _selectedIndex = 1;//设置默认选中第2个按钮
        }
        if (self.subviews.count == _selectedIndex) {
            [self buttonClicked:btn];
        }
    }
}

- (void)buttonClicked:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectButtonFrom:to:)]) {
        [self.delegate tabBar:self didSelectButtonFrom:self.selectedBtn.tag to:sender.tag];
    }
    
    //通话删除按钮点击
    if ([sender.titleLabel.text isEqualToString:CYTabBarTitleD] || [sender.titleLabel.text isEqualToString:CYTabBarTitleE]) {
        if ([self.delegate respondsToSelector:@selector(tabBar:buttonClicked:)]) {
            [self.delegate tabBar:self buttonClicked:sender];
        }
    }else {
        //设置按钮选中状态
        self.selectedBtn.selected = NO;
        sender.selected = YES;
        self.selectedBtn = sender;
    }
    
}

- (void)selectButtonWithIndex:(NSInteger)index {
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectButtonFrom:to:)]) {
        [self.delegate tabBar:self didSelectButtonFrom:self.selectedBtn.tag to:index];
    }
}

/**
 *  使用addsubview方法时调用该方法,设置tabBarButton的frame,ios9中当所有子view添加完成后才调用
 */
- (void)layoutSubviews {
    [super layoutSubviews];
    if (!self.callBtnShow) {
        CGFloat count = self.subviews.count;
        CGFloat tabBarW = self.frame.size.width/count;
        CGFloat tabBarH = self.frame.size.height;
        CGFloat tabBarY = 0;
        for (int i = 0; i < count; i++) {
            UIButton *tabBtn = self.subviews[i];
            tabBtn.tag = i;
            CGFloat tabBarX = tabBarW * i;
            tabBtn.frame = CGRectMake(tabBarX, tabBarY, tabBarW, tabBarH);
        }
    }
}

- (void)setSelectedTabBarWithIndex:(NSInteger)index {
    for (UIButton *btn in self.subviews) {
        if (btn.tag == index) {
            self.selectedBtn.selected = NO;
            btn.selected = YES;
            self.selectedBtn = btn;
        }
    }
}

#pragma mark - 键盘点击通知，tabbar改变
- (void)callBtnChange:(NSNotification *)noti {
    NSString *show = noti.userInfo[CYEditTabBarShow];
    self.callBtnShow = show.boolValue;
    [self layoutSubviews];
}
@end
